using System;
using System.Data;
using vs.Core;

namespace Forum
{
    public partial class member_info : System.Web.UI.UserControl
    {
        private void LoadData(string username)
        {
            string strSQL = "SELECT     u.UserName, u.Fullname, u.Email, u.RegistedDate, m.* "
                + " FROM USR_Users u LEFT OUTER JOIN FORUM_Member m ON u.UserName = m.UserName "
                + " WHERE u.UserName=@UserName";
            DataParameter p = new DataParameter("@UserName", username);
            DataTable dt = DataFactory.SelectTable(strSQL, p);

            if (dt.Rows.Count > 0)
            {
                string email = dt.Rows[0]["Email"].ToString();
                lnkEmail.NavigateUrl = "mailto:" + email;
                lnkEmail.Text = email;

                lblFullName.Text = dt.Rows[0]["Fullname"].ToString();
                lblPosts.Text = dt.Rows[0]["Posts"].ToString();
                lblRegistedDate.Text = dt.Rows[0]["RegistedDate"].ToString();
                lblSlogan.Text = dt.Rows[0]["Slogan"].ToString();
                lblUserName.Text = dt.Rows[0]["UserName"].ToString();

                string imageurl = dt.Rows[0]["ImageUrl"].ToString();
                if (imageurl == "") imageurl = ResolveUrl("~/images/noimage.gif");
                imgPhoto.ImageUrl = imageurl;

                if(MemberController.EditMemberInfoRight(dt.Rows[0]["UserName"].ToString()))
                {
                    lnkEdit.Visible = true;
                    lnkEdit.NavigateUrl = UrlProvider.AdminUrl("fr_member", "edit", username);
                }
            }
        }

        private void CheckExist(string username)
        {
            MemberController db = new MemberController();
            if (!db.Exist(username))
            {
                MemberInfo mem = new MemberInfo();
                mem.UserName = username;
                db.Insert(mem);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    if (Request.QueryString["ID"] == null) return;

                    string username = Request.QueryString["ID"];
                    LoadData(username);
                    CheckExist(username);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}